package com.antwork.common.web.jsp;

import com.antwork.common.entity.Pagination;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

public class PaginationTag extends AbstractTag {
	private static final long serialVersionUID = 2061499819745223032L;
	private static HashMap<String, PaginationConfig> configMap = new HashMap();
	private static final int DEFAULT_SIZE = 10;
	private static final String DEFAULT_ALIGN = "left";
	private String name;
	private Pagination<?> pagination = null;
	private String uri;

	public void setName(String name) {
		this.name = name;
	}

	public int doStartTag() throws JspException {
		PaginationConfig config = (PaginationConfig) configMap.get(this.name);
		if (config == null) {
			try {
				config = loadPaginationConfig();
			} catch (Exception e) {
				throw new JspException(e);
			}
			configMap.put(this.name, config);
		}
		int size = config.getSize();
		ServletRequest request = this.pageContext.getRequest();
		this.pagination = ((Pagination) request.getAttribute("pagination"));
		this.uri = ((String) request.getAttribute("uri"));
		int currentPage = this.pagination.getCurrentPage();
		int nextPageNo;
		int prePageNo;
		if (currentPage <= size / 2) {
			prePageNo = 0;
			if (this.pagination.getPageCount() < size + 1)
				nextPageNo = this.pagination.getPageCount() + 1;
			else
				nextPageNo = size + 1;
		} else {
			prePageNo = currentPage - size / 2;
			nextPageNo = prePageNo + size + 1;
			if (nextPageNo > this.pagination.getPageCount() + 1) {
				nextPageNo = this.pagination.getPageCount() + 1;
				prePageNo = nextPageNo - size - 1;
			}
		}
		String align = config.getAlign();
		String alignStr;
		if (align.equals("right")) {
			alignStr = " pagination-right";
		} else {
			if (align.equals("left"))
				alignStr = "";
			else
				alignStr = " pagination-centered";
		}
		StringBuffer htmlStr = new StringBuffer("<div class=\"pagination"
				+ alignStr + "\">");
		if (align.equals("right")) {
			String append = config.getAppend();
			if (append != null) {
				htmlStr.append(append);
			}
			 htmlStr.append("<span>" + this.pagination.getTotalCount() + "条/" + this.pagination.getPageCount() + "页</span>");
//			htmlStr.append("<span>" + this.pagination.getTotalCount() + "��/"
//					+ this.pagination.getPageCount() + "ҳ</span>");
		}
		htmlStr.append("<ul id=\"pageUL\">");
		htmlStr.append("<li><a href=\"###\" onclick=\"prePagination("
				+ prePageNo + ", " + this.pagination.getPageCount() + ", "
				+ size + ",'" + this.uri + "')\">Prev</a></li>");

		for (int i = prePageNo + 1; i < nextPageNo; ++i) {
			String activeStr;
			if (currentPage == i)
				activeStr = " class=\"active\"";
			else {
				activeStr = "";
			}
			htmlStr.append("<li" + activeStr + "><a href=\""
					+ setPageForURI(this.uri, i) + "\">" + i + "</a></li>");
		}
		htmlStr.append("<li><a href=\"###\" onclick=\"nextPagination("
				+ nextPageNo + ", " + this.pagination.getPageCount() + ", "
				+ size + ",'" + this.uri + "')\">Next</a></li>");
		htmlStr.append("</ul>");

		if (!align.equals("right")) {
			 htmlStr.append("<span>" + this.pagination.getTotalCount() + "条/" + this.pagination.getPageCount() + "页</span>");
//			htmlStr.append("<span>" + this.pagination.getTotalCount() + "��/"
//					+ this.pagination.getPageCount() + "ҳ</span>");
			String append = config.getAppend();
			if (append != null) {
				htmlStr.append(append);
			}
		}
		htmlStr.append("</div>");

		JspWriter writer = this.pageContext.getOut();
		try {
			writer.write(htmlStr.toString());
		} catch (IOException e) {
			throw new JspException(e);
		}

		return 6;
	}

	private PaginationConfig loadPaginationConfig() throws Exception {
		Properties prop = loadConfig(this.name);
		PaginationConfig config = new PaginationConfig();
		String size = prop.getProperty("pagination.size");
		if (size == null)
			config.setSize(10);
		else {
			config.setSize(Integer.parseInt(size));
		}
		String align = prop.getProperty("pagination.align");
		if (align == null)
			config.setAlign("left");
		else {
			config.setAlign(align);
		}
		String append = prop.getProperty("pagination.append");
		if (append != null) {
			config.setAppend(append);
		}
		return config;
	}

	private String setPageForURI(String uri, int page) {
		int index = uri.indexOf("p=");
		String newURI;
		if (index != -1) {
			String s1 = uri.substring(0, index);
			String s2 = uri.substring(index, uri.length());
			String s3 = null;
			index = s2.indexOf("&");
			if (index != -1)
				s3 = s2.substring(index, s2.length());
			if (s3 != null)
				newURI = s1 + "p=" + page + s3;
			else
				newURI = s1 + "p=" + page;
		} else {
			if (uri.indexOf("?") != -1)
				newURI = uri + "&p=" + page;
			else {
				newURI = uri + "?p=" + page;
			}
		}
		return newURI;
	}
}